package com.tz.cartelo.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tz.cartelo.BaseApplication;
import com.tz.cartelo.R;
import com.tz.cartelo.adapter.ManagerStockAdapter;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.result.ResultGoodsClassifyDetails;
import com.tz.cartelo.util.AES;
import com.tz.cartelo.util.CommonUtils;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/4/16.
 */

public class ManagerStockActivity extends DataLoadActivity implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    private static final String TAG = "ManagerStockActivity";
    private BGARefreshLayout mRefreshLayout;
    private Api mApi;
    private int mPageNo;
    private boolean mSaleStatus;
    private static final String SALE_BIG = "1";//1销量 大-小
    private static final String SALE_SMALL = "-1";//-1销量 小-大
    private boolean mStockStatus;
    private static final String STOCK_BIG = "2";//2库存 大-小
    private static final String STOCK_SMALL = "-2";//-2库存 小-大
    private boolean mPriceStatus;
    private static final String PRICE_BIG = "3";//3价格 大-小
    private static final String PRICE_SMALL = "-3";//-3价格 小-大
    private String mSort;
    private TextView mGoodsCenterSale, mGoodsCenterStock, mGoodsCenterPrice;
    private LinearLayout mGoodsClassifyLayout, mGoodsClassifyNoGoodsLayout, mGoodsCenterSaleLayout, mGoodsCenterStockLayout, mGoodsCenterPriceLayout;
    private ImageView mGoodsCenterSaleArrowTop, mGoodsCenterSaleArrowDown, mGoodsCenterStockArrowTop, mGoodsCenterStockArrowDown, mGoodsCenterPriceArrowTop, mGoodsCenterPriceArrowDown;
    private ManagerStockAdapter mManagerStockAdapter;
    private List<GoodsList> mGoodsListList;
    private boolean mCanLoadMore = true;
    private TextView mManagerStockSearchText;
    private ListView managerStockList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_manager_stock;
    }

    @Override
    protected void init() {
        BaseApplication.addActivity(this);
        mRefreshLayout = (BGARefreshLayout) findViewById(R.id.fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, this);

        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.manager_stock_title);
        LinearLayout manager_stock_search_layout = (LinearLayout) findViewById(R.id.manager_stock_search_layout);
        findViewById(R.id.manager_stock_classify_img).setOnClickListener(this);
        mManagerStockSearchText = (TextView) findViewById(R.id.manager_stock_search_text);
        mManagerStockSearchText.setOnClickListener(this);

        if (getIntent().getFlags() == Key.CLASSIFY_STOCK) {
            titleText.setText(getIntent().getStringExtra(Key.GOODS_CLASSIFY_TITLE));
            manager_stock_search_layout.setVisibility(View.GONE);
            mManagerStockSearchText.setText(getIntent().getStringExtra(Key.GOODS_CLASSIFY_TITLE));
        }

//        mManagerStockSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//                    if (mManagerStockSearchText.getText().toString().trim().isEmpty()) {
//                        showToast(R.string.goods_classify_search_empty);
//                    } else {
//                        mPageNo = 1;
//                        mGoodsListList.clear();
//                        mCanLoadMore = true;
//                        mApi = Api.STOCK_MANAGER;
//                        loadData(mApi, true);
//                    }
//                }
//                return true;
//            }
//        });

        mGoodsClassifyLayout = (LinearLayout) findViewById(R.id.goods_classify_layout);
        mGoodsClassifyNoGoodsLayout = (LinearLayout) findViewById(R.id.goods_classify_no_goods_layout);

        mGoodsCenterSale = (TextView) findViewById(R.id.goods_center_sale);
        mGoodsCenterStock = (TextView) findViewById(R.id.goods_center_stock);
        mGoodsCenterPrice = (TextView) findViewById(R.id.goods_center_price);
        mGoodsCenterSaleLayout = (LinearLayout) findViewById(R.id.goods_center_sale_layout);
        mGoodsCenterSaleArrowTop = (ImageView) findViewById(R.id.goods_center_sale_arrow_top);
        mGoodsCenterSaleArrowDown = (ImageView) findViewById(R.id.goods_center_sale_arrow_down);
        mGoodsCenterStockLayout = (LinearLayout) findViewById(R.id.goods_center_stock_layout);
        mGoodsCenterStockArrowTop = (ImageView) findViewById(R.id.goods_center_stock_arrow_top);
        mGoodsCenterStockArrowDown = (ImageView) findViewById(R.id.goods_center_stock_arrow_down);
        mGoodsCenterPriceLayout = (LinearLayout) findViewById(R.id.goods_center_price_layout);
        mGoodsCenterPriceArrowTop = (ImageView) findViewById(R.id.goods_center_price_arrow_top);
        mGoodsCenterPriceArrowDown = (ImageView) findViewById(R.id.goods_center_price_arrow_down);

        mGoodsCenterSaleLayout.setOnClickListener(this);
        mGoodsCenterStockLayout.setOnClickListener(this);
        mGoodsCenterPriceLayout.setOnClickListener(this);

        managerStockList = (ListView) findViewById(R.id.manager_stock_list);
        mGoodsListList = new ArrayList<>();
        mManagerStockAdapter = new ManagerStockAdapter(this, mGoodsListList);
        managerStockList.setAdapter(mManagerStockAdapter);
        managerStockList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString(Key.GOODS_ID, mGoodsListList.get(i).getGoodsId());
                switchActivity(GoodsDetailsActivity.class, bundle, Key.CLASSIFY_STOCK);
            }
        });

        mPageNo = 1;
        mSaleStatus = true;
        mSort = SALE_BIG;
        mApi = Api.STOCK_MANAGER;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.manager_stock_search_text:
                switchActivity(ManagerStockSearchActivity.class, null);
                break;
            case R.id.manager_stock_classify_img:
                switchActivity(GoodsClassifyActivity.class, null, Key.CLASSIFY_STOCK);
                break;
            case R.id.goods_center_sale_layout:
                if (mSaleStatus) {
                    mSaleStatus = false;
                    mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                    mSort = SALE_SMALL;
                } else {
                    mSaleStatus = true;
                    mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                    mSort = SALE_BIG;
                }
                mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_frame_gray);
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_fill_gray);
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_fill_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.black));
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.white));
                mStockStatus = false;
                mPriceStatus = false;
                mPageNo = 1;
                mGoodsListList.clear();
                mApi = Api.STOCK_MANAGER;
                loadData(mApi, true);
                break;
            case R.id.goods_center_stock_layout:
                if (mStockStatus) {
                    mStockStatus = false;
                    mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                    mSort = STOCK_SMALL;
                } else {
                    mStockStatus = true;
                    mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                    mSort = STOCK_BIG;
                }
                mSaleStatus = false;
                mPriceStatus = false;
                mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_fill_gray);
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_frame_gray);
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_fill_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.black));
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.white));
                mPageNo = 1;
                mGoodsListList.clear();
                mApi = Api.STOCK_MANAGER;
                loadData(mApi, true);
                break;
            case R.id.goods_center_price_layout:
                if (mPriceStatus) {
                    mPriceStatus = false;
                    mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                    mSort = PRICE_SMALL;
                } else {
                    mPriceStatus = true;
                    mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                    mSort = PRICE_BIG;
                }
                mSaleStatus = false;
                mStockStatus = false;
                mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_fill_gray);
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_fill_gray);
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_frame_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.black));
                mPageNo = 1;
                mGoodsListList.clear();
                mApi = Api.STOCK_MANAGER;
                loadData(mApi, true);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case STOCK_MANAGER:
//                goodsCategoryId
                if (getIntent().getFlags() == Key.CLASSIFY_STOCK) {
                    params.addParam("goodsCategoryId", getIntent().getStringExtra(Key.GOODS_CLASSIFY_ID));
//                    params.addParam("searchName", getIntent().getStringExtra(Key.GOODS_CLASSIFY_TITLE));
                } else {
                    params.addParam("searchName", mManagerStockSearchText.getText().toString().trim());
                }
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                params.addParam("pageNo", mPageNo + "");
//                params.addParam("searchName", mManagerStockSearchText.getText().toString().trim());
                params.addParam("sort", mSort);
                String time = System.currentTimeMillis() + "";
                params.addParam("time", time);

                List<String> dataList = new ArrayList<>();
                dataList.add(PreferencesUtils.getString(Key.PER_USER_ID, ""));
                dataList.add(time);
                try {
                    params.addParam("sign", AES.createASE(dataList));
                } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case STOCK_MANAGER:
                //结束刷新和加载
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endLoadingMore();

                ResultGoodsClassifyDetails resultGoodsClassifyDetails = fromJson(response, ResultGoodsClassifyDetails.class);
                if (!resultGoodsClassifyDetails.isSuccess()) {
                    showToast(resultGoodsClassifyDetails.getMessage());
                    return;
                }
                mGoodsListList.addAll(resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList());
                mManagerStockAdapter.notifyDataSetChanged();

                if (resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList().isEmpty() && mGoodsListList.isEmpty())
                    mGoodsClassifyNoGoodsLayout.setVisibility(View.VISIBLE);
                else
                    mGoodsClassifyLayout.setVisibility(View.VISIBLE);

                if (mPageNo > 1) {
                    if (resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList().isEmpty()) {
                        mCanLoadMore = false;
                        mPageNo--;
                    }
                }

                managerStockList.setSelection(0);
                break;
            default:
                break;
        }
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mPageNo = 1;
        mGoodsListList.clear();
        mCanLoadMore = true;
        mApi = Api.STOCK_MANAGER;
        loadData(mApi, true);
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (!mCanLoadMore) {
            mRefreshLayout.endLoadingMore();
            return false;
        } else {
            mPageNo++;
            mApi = Api.STOCK_MANAGER;
            loadData(mApi, true);
            return true;
        }
//        mRefreshLayout.endLoadingMore();
//        return false;
    }

}
